package jsonUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import model.TraceVO;

/**
 * 
 * <strong>类描述：</strong> gson 解析 json 工具类<br/>
 * <strong>创建者：</strong> ZZF<br/>
 * <strong>项目名称：</strong> jsonUtils<br/>
 * <strong>创建时间：</strong> 2018年2月1日 上午10:25:05<br/>
 * <strong>版本号：</strong> v1.0<br/>
 */
public class GsonUtils {

	private GsonUtils() {
	}

	/*
	 */
	private static final TypeAdapter<String> STRING = new TypeAdapter<String>() {
		public String read(JsonReader reader) throws IOException {
			if (reader.peek() == JsonToken.NULL) {
				reader.nextNull();
				return "";
			}
			return reader.nextString();
		}

		public void write(JsonWriter writer, String value) throws IOException {
			if (value == null) {
				writer.value("");
				return;
			}
			writer.value(value);
		}
	};
	private static Gson gson = null;

	public static Gson getInstance() {
		if (gson == null) {
			GsonBuilder gsonBuilder = new GsonBuilder();
			gsonBuilder.registerTypeAdapter(String.class, STRING);
			gson = gsonBuilder.create();
		}
		return gson;
	}

	public static String toJson(Object object) {
		return getInstance().toJson(object);
	}

	public static Object fromJson(String json, Class<?> clazz) {
		Object obj = null;
		obj = getInstance().fromJson(json, clazz);
		return obj;
	}

	public static <T> List<T> fromJsonToList(String json, Class<T[]> clazz) {
		T[] array = getInstance().fromJson(json, clazz);
		return Arrays.asList(array);
	}

	public static <T> ArrayList<T> fromJsonToArrayList(String json, Class<T> clazz) {
		Type type = new TypeToken<ArrayList<JsonObject>>() {
		}.getType();
		ArrayList<JsonObject> jsonObjects = getInstance().fromJson(json, type);

		ArrayList<T> arrayList = new ArrayList<T>();
		for (JsonObject jsonObject : jsonObjects) {
			arrayList.add(getInstance().fromJson(jsonObject, clazz));
		}
		return arrayList;
	}

	//测试物流信息转化实体
	public static void main(String[] args) {
		String jsonStr = "{\"Message\":\"ok\",\"status\":\"1\",\"state\":\"3\",\"data\":[{\"time\":\"2012-07-07 13:35:14\",\"context\":\"客户已签收\"},{\"time\":\"2012-07-07 09:10:10\",\"context\":\"离开 [北京_房山营业所_石景山营业厅] 派送中，递送员[温]，电话[]\"},{\"time\":\"2012-07-06 19:46:38\",\"context\":\"到达 [北京_房山营业所_石景山营业厅]\"},{\"time\":\"2012-07-06 15:22:32\",\"context\":\"离开 [北京_房山营业所_石景山营业厅] 派送中，递送员[温]，电话[]\"},{\"time\":\"2012-07-06 15:05:00\",\"context\":\"到达 [北京_房山营业所_石景山营业厅]\"},{\"time\":\"2012-07-06 13:37:52\",\"context\":\"离开 [北京_同城中转站] 发往 [北京_房山营业所_石景山营业厅]\"},{\"time\":\"2012-07-06 12:54:41\",\"context\":\"到达 [北京_同城中转站]\"},{\"time\":\"2012-07-06 11:11:03\",\"context\":\"离开 [北京运转中心_航空_驻站班组] 发往 [北京_同城中转站]\"},{\"time\":\"2012-07-06 10:43:21\",\"context\":\"到达 [北京运转中心_航空_驻站班组]\"},{\"time\":\"2012-07-05 21:18:53\",\"context\":\"离开 [福建_厦门支公司] 发往 [北京运转中心_航空]\"},{\"time\":\"2012-07-05 20:07:27\",\"context\":\"已取件，到达 [福建_厦门支公司]\"}]}";
		TraceVO trace = (TraceVO) fromJson(jsonStr, TraceVO.class);
//		System.out.println(trace.getMessage());
//		System.out.println(trace.getState());
//		System.out.println(trace.getData().size());
		System.out.println(toJson(trace));
	}
}
